package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import utils.DriverWrapper;
import utils.LoggTest;

import org.openqa.selenium.NoSuchElementException;


/**
 * Created with IntelliJ IDEA.
 * User: kvasolka
 * Date: 5/26/14
 * Time: 11:46 PM
 * To change this template use File | Settings | File Templates.
 */
public class HomePage {

    public static DriverWrapper driver;
    public final String URL = "http://rozetka.com.ua/";
    protected final By searchField = By.className("header-search-input-text");
    protected final By toComparison = By.name("comparison_catalog");    //By.name("(//div[@name='comparison_catalog'])[1]//a]");
    protected final String productFoundPattern = "//a[contains(text(), '%s')]" ;
    protected final By comparisonDetails = By.name("tocomparison");
    protected final By checkElement = By.id("compare-menu");


    public HomePage(DriverWrapper driver) {
        this.driver = driver;
        PageFactory.initElements(driver,this);
    }

    public void open() {
        driver.get(URL);
    }

    public boolean isOpen() {
        return  driver.getCurrentUrl().equals(URL);
    }

    public boolean searchProduct(String productName) {
        try {
            LoggTest.info("searching product: " + productName);
            driver.findElement(searchField).clear();
            driver.findElement(searchField).sendKeys(productName);
            driver.findElement(searchField).sendKeys(Keys.RETURN);
            return driver.findElement(By.xpath(String.format(productFoundPattern, productName))).isDisplayed();
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    public boolean addProductToCompare() {

        WebElement elem;
        try {
            LoggTest.info("Adding product to comparison");
            elem = driver.findElement(comparisonDetails);
            elem.click();
        }
        catch(Exception e) {
            e.printStackTrace();
            return false;
        }
        return driver.findElement(toComparison).isDisplayed();
    }

//    public boolean productPresent(String productName){
//        WebDriverWait wait = new WebDriverWait(driver, 20);
//        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(String.format(productFoundPattern, productName))));
//        return driver.findElement(By.xpath(String.format(productFoundPattern, productName))).isDisplayed();
//    }

    public boolean compareProducts(String prod1, String prod2) {

        LoggTest.info("Comparision of: " + prod1 + " and " + prod2);

        try {
            WebElement toComparisonDiv = driver.findClickableElement(toComparison);
            WebElement toComparisonLink = toComparisonDiv.findElement(By.tagName("a"));
            toComparisonLink.click();
            //return driver.findElement(By.partialLinkText(prod1)).isDisplayed()&& driver.findElement(By.partialLinkText(prod2)).isDisplayed();

        }
        catch (NoSuchElementException e)  {
            return false;
        }
        catch (Exception e) {
            //e.printStackTrace();
            return false;
        }
        return driver.findElement(By.partialLinkText(prod1)).isDisplayed()&& driver.findElement(By.partialLinkText(prod2)).isDisplayed();

    }



}
